<!-- 处方管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入处方名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable @change="handleQuery">
          <el-option label="经典方" :value="1"></el-option>
          <el-option label="协定方" :value="2"></el-option>
          <!-- <el-option label="医师方" :value="3"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="处方类型" prop="prescriptionType">
        <el-select v-model="queryParams.prescriptionType" placeholder="请选择" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.tcm_prescript_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          v-hasPermi="['saas:prescription:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="openImport()"
          >批量导入</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="处方名称" align="center" prop="name" />
      <el-table-column label="处方分类" align="center" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.type === 1" type="info">经典方</el-tag>
          <el-tag v-else-if="row.type === 2" type="info">协定方</el-tag>
          <el-tag v-else type="info">医师方</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处方类型" align="center" width="100">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.tcm_prescript_type" :value="String(row.prescriptionType)" />
        </template>
      </el-table-column>
      <el-table-column label="功效" align="center" prop="effect" />
      <el-table-column label="主治" align="center" prop="mainTreat" width="400" />
      <el-table-column label="处方来源" align="center" prop="source" />
      <el-table-column label="用法" align="center" prop="usage" />
      <el-table-column label="状态" align="center" width="140">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.status"
            :active-value="1"
            active-text="启用"
            :inactive-value="0"
            inactive-text="禁用"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleOpen(scope.row)"
            v-hasPermi="['saas:prescription:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['saas:prescription:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <PrescriptionEdit ref="PrescriptionEdit" @refresh="getList" />

    <ImportModal
      ref="ImportModal"
      title="导入处方"
      url="/tool/importData/tcm/importPrescript"
      templateUrl="https://onelive.obs.cn-south-1.myhuaweicloud.com/import-temp/%E5%A4%84%E6%96%B9%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx"
      @refresh="resetQuery"
      @openErr="openErrorModal"
    />

    <el-dialog
      width="800px"
      title="本次导入数据部分失败，详情请查看失败原因"
      :visible.sync="errorVisible"
      @close="errorVisible = false"
    >
      <el-table :data="errorList">
        <el-table-column label="处方名称" align="center" prop="name" />
        <el-table-column label="处方分类" align="center" prop="type" />
        <el-table-column label="功效" align="center" prop="effect" show-overflow-tooltip />
        <el-table-column label="主治" align="center" prop="mainTreat" show-overflow-tooltip />
        <el-table-column label="处方来源" align="center" prop="source" />
        <el-table-column label="药材" align="center" prop="materialsItem" show-overflow-tooltip />
        <el-table-column label="导入失败原因" align="center" prop="reason" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getPrescriptList, deletePrescript, updatePrescript } from "@/api/saas/tcm.js";
import PrescriptionEdit from './prescriptionEdit'
import ImportModal from "@/components/importModal";

export default {
  name: "Prescription",
  dicts: [
    "tcm_prescript_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        type: undefined,
        prescriptionType: undefined
      },
      // 总条数
      total: 0,
      dataList: [],
      errorVisible: false,
      errorList: []
    };
  },
  components: {
    PrescriptionEdit,
    ImportModal
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getPrescriptList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
      }).finally(()=>{this.loading = false});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(row) {
      this.$refs.PrescriptionEdit.openModal(row);
    },
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该处方吗？")
        .then(function () {
          return deletePrescript(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 修改状态
    changeStatus(row) {
      updatePrescript({id: row.id, status: row.status}).then(res=>{
        this.$message.success('状态修改成功');
      }).catch((err)=>{
        row.status = row.status ? 0 : 1; // 修改失败,复位
      })
    },
    openImport() {
      this.$refs.ImportModal.openModal();
    },
    openErrorModal(list) {
      this.errorList = list;
      this.errorVisible = true;
    }
  }
};
</script>


